//app.js

App({


  onLaunch: function () {
    console.log("app的onlaunch")
    //登录流程
    var that = this

      

  },

  getUserInfo: function (cb) {
    var that = this
    if (that.globalData.openid) {
      console.log("全局变量中有openid")
      typeof cb == "function" && cb(that.globalData.userInfo, that.globalData.openid)
    } else {
      wx.login({
        success:function(res){
          console.log(res.code)
          if(res.code){
            wx.request({
              url: 'https://17fx.com/weapp/api/logingo.html',
              data: {
                code: res.code
              },
              method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
              header: {'content-type': 'application/json'}, // 设置请求的 header
              success: function (res) {
                if(res.data.unionid == null){
                  wx.login({//login流程
                    success: function (res) {//登录成功
                      console.log(res)
                      if (res.code) {
                        var code = res.code;
                        wx.getUserInfo({//getUserInfo流程
                          success: function (res2) {//获取userinfo成功
                            console.log(res2);
                            //var encryptedData = encodeURIComponent(res2.encryptedData);//一定要把加密串转成URI编码
                            var encryptedData = res2.encryptedData;
                            var iv = res2.iv;
                            var signature = res2.signature;
                            var rawData = res2.rawData;
                            console.log(res2.userInfo)
                            that.globalData.userInfo = res2.userInfo

                            //请求自己的服务器
                            // Login(code,encryptedData,iv,that);
                            //创建一个dialog
                            wx.showToast({
                              title: '正在登录...',
                              icon: 'loading',
                              duration: 10000
                            });
                            //请求服务器
                            wx.request({
                              url: 'https://17fx.com/weapp/api/login.html',
                              data: {
                                code: code,
                                encryptedData: encryptedData,
                                iv: iv,
                                rawData: rawData,
                                signature: signature,

                              },
                              method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
                              header: {
                                'content-type': 'application/json'
                              }, // 设置请求的 header
                              success: function (res) {
                                // success
                                wx.hideToast();
                                console.log('登录服务器返回1');

                                //更新用户数据 调用应用实例的方法获取全局数据
                                that.globalData.openid = res.data.openid
                                that.globalData.userid = res.data.userid
                                that.globalData.subscribe = res.data.subscribe

                                console.log(res.data)

                                typeof cb == "function" && cb(that.globalData.userInfo, res.data.openid)
                              },
                              fail: function () {
                                // fail
                                // wx.hideToast();
                                console.log('登录服务器失败33');
                              },
                              complete: function () {
                                // complete
                              }
                            })
                          }
                        })

                      } else {
                        console.log('获取用户登录态失败！22' + res.errMsg)
                      }
                    }
                  })
                }else{
                  //更新用户数据 调用应用实例的方法获取全局数据
                  that.globalData.openid = res.data.openid
                  that.globalData.userid = res.data.userid
                  that.globalData.subscribe = res.data.subscribe

                  console.log(res.data)
                  wx.getUserInfo({
                    success:function(res){
                      that.globalData.userInfo = res.userInfo
                    }
                  })
                  typeof cb == "function" && cb(that.globalData.userInfo, res.data.openid)
                }
                
              },
              fail: function () {
                console.log('登录服务器失败11');
              },
              complete: function () {}
            })

          }else{
            console.log("code获取失败11")
          }
        }
      })

    }
  },

  globalData: {
    userInfo: null,
    openid: null,
    session_id: null,
    isLogin: null,
    latitude:null,
    longitude:null,
    city_id:2,
    userid:0,
    suid:0,
    subscribe:1
  }
})



